package com.air.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author shent
 */
public class JUCDemo {

    private static int total = 0;
    private static Object object = new Object();
    private static ReentrantLock lock = new ReentrantLock(false);//是否开启公平锁

    public static void main(String[] args) throws InterruptedException {
        //倒计时锁存器
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                lock.lock();
                try {
                    countDownLatch.await();
                    for (int j = 0; j < 1000; j++) {
                        total++;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }).start();
        }
        Thread.sleep(500);
        countDownLatch.countDown();
        Thread.sleep(2000);
        System.out.println(total);
    }
}
